﻿Imports System.Web
Imports System.Web.Services
Imports System.Net
Imports System.Drawing.Imaging
Imports System.IO

Public Class ConvertGif
    Implements IHttpHandler

    Private _context As HttpContext
    Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        _context = context
        context.Response.ContentType = "image/jpeg"
        context.Response.BinaryWrite(GetBufferFromImage(context.Request("link")))
    End Sub
    Private Function GetBufferFromImage(ByVal imageLnk As String) As Byte()
        Dim request = HttpWebRequest.Create(_context.Server.MapPath("~/" & imageLnk))
        Dim response = request.GetResponse()
        Dim myImage = Drawing.Image.FromStream(response.GetResponseStream())
        Dim ImageStream As New MemoryStream
        myImage.Save(ImageStream, ImageFormat.Jpeg)
        ImageStream.Position = 0
        Dim buffer(ImageStream.Length) As Byte
        ImageStream.Read(Buffer, 0, ImageStream.Length)
        Return Buffer
    End Function
    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class